### REPLICATION FILE -- APPENDIX -- STUDY 2, PERCEPTIONS
### Homola, Rogowski, Sinclair, Torres, Tucker, and Webster
### "Through the Ideology of the Beholder: How Ideology Shapes Perceptions of Partisan Groups"
### Political Science Research and Methods

rm(list=ls())
library(xtable); library(stargazer)

# log file/sink
sink("HRSTTW_Stereotypes_Study2_Appendix_R.txt")

data_full <- read.csv("HRSTTW_Stereotypes_Study2_Appendix.csv")

index <- c("FEMALE", "WHITE", "age1829", "age3044", "age4559", "age60",
           "LessHighSchool", "HighSchool", "CollegeMore", "IncLess30", "Inc3049", "Inc5079", "IncMore80",
           "Metro", "Northeast", "Midwest", "South", "West",
           "Conservatism", "Ideology", "Democrat", "Republican", "Independent",
           "PolKnow","IntNot", "IntNotVery", "IntSomewhat", "IntVery")

get.diff <- function(ttobj){
  est.tt <- ttobj$estimate
  star <- ifelse(ttobj$p.value<=0.05, "*", "")
  return(paste0(round(est.tt[2]-est.tt[1],2), star))
}

final_mat <- cbind(round(apply(data_full[data_full$both==1,index],2,function(x) mean(x, na.rm=TRUE)),2),
                   round(apply(data_full[data_full$wave1==1,index],2,function(x) mean(x, na.rm=TRUE)),3),
                   do.call(rbind,lapply(index, function(x) get.diff(t.test(data_full[data_full$both==1,x], data_full[data_full$wave1==1,x])))),
                   round(apply(data_full[data_full$wave2==1,index],2,function(x) mean(x, na.rm=TRUE)),2),
                   do.call(rbind,lapply(index, function(x) get.diff(t.test(data_full[data_full$both==1,x], data_full[data_full$wave2==1,x])))))


# Table B.1: Descriptive Statistics
stargazer(data_full[data_full$both==1,index], summary = TRUE, omit.summary.stat=c("n", "p25", "p75"), title="Table B.1")

# Table B.2: Descriptive Statistics and Differences Between Waves
colnames(final_mat) <- c('Both waves', 'Wave 1', 'Both waves - Wave 1', 'Wave 2', 'Both waves - Wave 2')
stargazer(final_mat[,c(1,3,5)], summary=FALSE, title="Table B.2")

# Respondents per wave
sum(data_full$wave1) # 1669
sum(data_full$wave2) # 1496
sum(data_full$both) # 1301

# Figure B.1: Operational Ideology by Party
pdf("FigureB1_Operation_Ideo_Party.pdf", height=6,width=8)
plot(density(data_full[data_full$Democrat==1,]$ideoS27_std, na.rm=T), col='dodgerblue', xlim=c(-2.2,2.75), 
     main="", xlab="Operational Conservatism")
lines(density(data_full[data_full$Republican==1,]$ideoS27_std, na.rm=T), col='firebrick')
lines(density(data_full[data_full$Independent==1,]$ideoS27_std, na.rm=T), col='green')
legend("topright", legend=c("Democrats", "Independents", "Republicans"), 
       col=c("dodgerblue", "green", "firebrick"), lty=1, bty="n")
dev.off()

print("Descriptives for Figure B.1")
round(c(mean(data_full[data_full$Democrat==1,]$ideoS27_std, na.rm=T), 
        sd(data_full[data_full$Democrat==1,]$ideoS27_std, na.rm=T)), 2)
round(c(mean(data_full[data_full$Republican==1,]$ideoS27_std, na.rm=T), 
        sd(data_full[data_full$Republican==1,]$ideoS27_std, na.rm=T)), 2)
round(c(mean(data_full[data_full$Independent==1,]$ideoS27_std, na.rm=T), 
        sd(data_full[data_full$Independent==1,]$ideoS27_std, na.rm=T)), 2)

sink()